import redis
from redis.sentinel import Sentinel

# 连接哨兵服务器(主机名也可以用域名)
sentinels = Sentinel([
    ('10.41.95.49', 26371),
    ('10.41.95.49', 26372),
    ('10.41.95.49', 26373),
    ('10.41.95.49', 26374),
], socket_timeout=1)


# 获取主服务器地址
master = sentinels.discover_master('master106')
print(master)


# 获取从服务器的地址
slave = sentinels.discover_slaves('master106')
print(slave)


# # 主服务器写入
# master = sentinels.master_for('master106', socket_timeout=1, db=0)
# w_ret = master.set('foo', 'bar')
# print(w_ret)


# # 从服务器读取
# slave = sentinels.slave_for('master106', socket_timeout=1, db=0)
# r_ret = slave.get('foo')
# print(r_ret)